using System;
using System.Collections;
using System.Web.Caching;

namespace JQuery.WebKit.Common.Caching
{
    public class MemoryCache : ICache
    {
        #region Hashtable _inner
        /// <summary>
        /// Get/Sets the _inner of the <see cref="UI.WebCommon.Caching.MemoryCache"/>
        /// </summary>
        Hashtable _inner;
        #endregion

        #region public MemoryCache()
        /// <summary>
        /// Initializes a new instance of the <b>MemoryCache</b> class.
        /// </summary>
        public MemoryCache()
        {
            _inner = new Hashtable();
        }
        #endregion

        #region public void AddItem(string key, object item)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="item"></param>
        public void AddItem(string key, object item)
        {
            this.AddItem(key, item, null, null);
        }
        #endregion

        #region public void AddItem(string key, object item, TimeSpan? expireTime, CacheItemRemovedCallback removedCallBack)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="item"></param>
        /// <param name="expireTime"></param>
        /// <param name="removedCallBack"></param>
        public void AddItem(string key, object item, TimeSpan? expireTime, CacheItemRemovedCallback removedCallBack)
        {
            lock (_inner)
            {
                _inner[key] = item;
            }
        }
        #endregion

        #region public bool RemoveItem(string key)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool RemoveItem(string key)
        {
            lock (_inner)
            {
                if (!_inner.ContainsKey(key))
                {
                    return false;
                }
                _inner.Remove(key);
                return true;
            }
        }
        #endregion

        #region public object GetItem(string key)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public object GetItem(string key)
        {
            lock (_inner)
            {
                return _inner[key];
            }
        }
        #endregion

        #region public bool UpdateItem(string key, object value)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool UpdateItem(string key, object value)
        {
            lock (_inner)
            {
                object item = _inner[key];
                if (item == null)
                {
                    return false;
                }
                else
                {
                    _inner[key] = value;
                    return true;
                }
            }
        }
        #endregion

    }
}
